//
//  MeViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/4.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "MeViewController.h"
#import "LoginNavigationController.h"
#import "MePageHeadView.h"
#import "AssetsListTableViewCell.h"
#import "MeCommunityViewController.h"
#import "MeSetViewController.h"
#import "UserCenter.h"
#import "taskUserManagerService.h"
#import "MeMessageViewController.h"
#import "QianBaoViewController.h"
#import "MeFeedbackViewController.h"
#import "MeSourceDetailViewController.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,strong)MePageHeadView *headView;
@property (nonatomic,strong)NSArray        *cellArray;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.title = @"个人中心";
    
    
    self.cellArray = @[@[@"我的钱包",@"GTE明细",@"GTK明细",@"我的社区",@"分享推广",@"资产管理",@"消息中心",@"反馈"],@[@"icon_me_mo",@"icon_me_gte",@"icon_me_gtk",@"icon_me_users",@"icon_me_multiple",@"icon_me_lists",@"icon_me_flag",@"icon_me_edit"]];
    [self.view addSubview:self.tableView];
    
    [self loadgeneralAuth];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self setBlackStatus:NO];
    
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginIfSuccess) name:Notification_login_ok_notice object:nil];
    }
    return self;
}


-(void)loginIfSuccess
{
    
    self.headView.lbname.text = [UserCenter shareCenter].user.userName != nil ?  [UserCenter shareCenter].user.userName : @"GTE会员";
    self.headView.urlImage.layer.masksToBounds = YES;
    self.headView.urlImage.layer.borderWidth = 1.0f;
    self.headView.urlImage.layer.borderColor = [UIColor clearColor].CGColor;
    self.headView.urlImage.layer.cornerRadius = 30.0;
    [self.headView.urlImage sd_setImageWithURL:[NSURL URLWithString:[UserCenter shareCenter].user.imageUrl] placeholderImage:[UIImage imageNamed:@"icon_avatar"]];
}

#pragma mark - loadData

///用户认证信息
-(void)loadgeneralAuth{
    
    [DKProgressHUD showLoading];
    [taskUserManagerService getCommunity:nil success:^(dataCommunityOverview *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.headView.labelStartRZ.text = [NSString stringWithFormat:@"%@级 %@",response.data.level,response.data.status];
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

#pragma mark - init
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

-(MePageHeadView *)headView
{
    if (!_headView) {
        _headView = [MePageHeadView getHeadView];
        [_headView.setButton addTarget:self action:@selector(setAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

#pragma mark - event
-(void)setAction
{
    [self.navigationController pushViewController:[MeSetViewController new] animated:YES];
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cellArray[0] count];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
    footerView.backgroundColor = COLOR_Background;
    UIView *roundView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, KScreenWidth - 30, 20)];
    roundView.backgroundColor = [UIColor whiteColor];
    UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:roundView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
    maskLayer.frame = roundView.bounds;
    maskLayer.path = maskPath.CGPath;
    roundView.layer.mask = maskLayer;
    [footerView addSubview:roundView];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 240;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AssetsListTableViewCell *cell = [AssetsListTableViewCell cellWithTableView:tableView];
    cell.lbtitle.text = self.cellArray[0][indexPath.row];
    cell.iamgeView.image = [UIImage imageNamed:self.cellArray[1][indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
